package com.helen.adapter;

import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.helen.entity.AddressListEntity;
import com.helen.entity.PersonPageItem;
import com.helen.shopping.R;
import com.helen.utils.MyLog;

import java.util.List;


/**
 * Created by yang on 2017/11/23.
 */

public class AddressListAdapter extends BaseQuickAdapter<AddressListEntity, BaseViewHolder> {
    private OnSetItemDefaultListener onSetItemDefaultListener;
    private OnSetItemEditListener onSetItemEditListener;
    private OnSetItemDeleteListener onSetItemDeleteListener;
    private int type = 0;  //0老的回收地址  1新的回收地址

    public AddressListAdapter(int layoutResId, List<AddressListEntity> data,int type) {
        super(layoutResId, data);
        this.type = type;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final AddressListEntity item) {
        try {
            helper.setText(R.id.tv_user_name, item.getName());
            helper.setText(R.id.tv_user_phone, item.getPhone());
            switch (type){
                case 0:
                    helper.setText(R.id.tv_detail_address, item.getCity() + item.getDetail());
                    String isDefault = item.getIsDefault();
                    helper.setChecked(R.id.rb_item_default, isDefault.equals("1") ? true : false);
                    break;
                case 1:
                    helper.setText(R.id.tv_detail_address, item.getCityName() + item.getDetail());
                    helper.setChecked(R.id.rb_item_default, item.getIsDefault().equals("true") ? true : false);
                    break;
            }



            RadioButton rb = (RadioButton) helper.getView(R.id.rb_item_default);
            TextView itemEdit = (TextView) helper.getView(R.id.tv_item_edit);
            TextView itemDelete = (TextView) helper.getView(R.id.tv_item_delete);

            rb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    switch (type) {
                        case 0:
                            if (onSetItemDefaultListener != null && item.getIsDefault().equals("0")) {
                                onSetItemDefaultListener.setOnDefaultListener(item, helper.getLayoutPosition());
                            }
                            break;
                        case 1:
                            if (onSetItemDefaultListener != null && item.getIsDefault().equals("false")) {
                                onSetItemDefaultListener.setOnDefaultListener(item, helper.getLayoutPosition());
                            }
                            break;
                    }

                }
            });

            itemEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onSetItemEditListener != null) {
                        onSetItemEditListener.setOnEditListener(item, helper.getLayoutPosition());
                    }
                }
            });


            itemDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onSetItemDeleteListener != null) {
                        onSetItemDeleteListener.setOnDeleteListener(item, helper.getLayoutPosition());
                    }
                }
            });

        } catch (Exception e) {
            MyLog.e("yang", "设置地址列表异常" + e.toString());
        }


    }

    public interface OnSetItemDefaultListener {
        void setOnDefaultListener(AddressListEntity addressListEntity, int position);
    }


    public void setOnItemDefaultListener(OnSetItemDefaultListener onItemDefaultListener) {

        this.onSetItemDefaultListener = onItemDefaultListener;
    }

    public interface OnSetItemEditListener {
        void setOnEditListener(AddressListEntity addressListEntity, int position);
    }

    public void setOnItemEditListener(OnSetItemEditListener onSetItemEditListener) {
        this.onSetItemEditListener = onSetItemEditListener;
    }


    public interface OnSetItemDeleteListener {
        void setOnDeleteListener(AddressListEntity addressListEntity, int position);
    }

    public void setOnItemDeleteListener(OnSetItemDeleteListener onItemDeleteListener) {
        this.onSetItemDeleteListener = onItemDeleteListener;
    }
}
